package com.canxin.service.impl;

import com.canxin.entity.ExamEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.canxin.dao.ScoreDao;
import com.canxin.entity.ScoreEntity;
import com.canxin.service.ScoreServiceI;
import com.canxin.tools.StringTools;

import weixin.cms.dao.CmsAdDao;

import java.util.List;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

@Service("scoreService")
@Transactional
public class ScoreServiceImpl extends CommonServiceImpl implements ScoreServiceI {

	@Autowired
	private ScoreDao scoreDao;
	
	@Override
	public List<ScoreEntity> getAllScoresOrderByExamDate(String studentId) {
		// TODO Auto-generated method stub
		return scoreDao.list(studentId);
	}

	@Override
	public List<ScoreEntity> getAllStudentScoresByExamId(String examId) {
		return scoreDao.getAllStudentScoresByExamId(examId);
	}

	@Override
	public ScoreEntity getScoresByStudentIdAndExamId(String studentId, String examId) {
		// TODO Auto-generated method stub
		//return (ScoreEntity) super.findHql("from ScoreEntity where studentid=? and examId=? and rank>0", studentId,examId).get(0);
		return scoreDao.getScoresByStudentIdAndExamId(studentId,examId);
	}

	@Override
	public List getStudentsCountBeyondParamsByExamId(String examId, String num) {
		// TODO Auto-generated method stub
		return scoreDao.getStudentCountBeyondParamsByExamId(examId, Integer.parseInt(num));
	}

	@Override
	public int getStudentAverageRankByStudentIdAndExamId(String studentId, List<ExamEntity> examList) {
		String examId = "";
		for(ExamEntity exam:examList)
		{
			examId = examId + "'" + exam.getExamid()+"',";
		}
		examId = examId.substring(0,examId.length()-1);
		return scoreDao.getStudentAverageRankByStudentIdAndExamId(studentId,examId);
	}

	@Override
	public int getStudentRankCountBeyondParamsByStudentId(String studentId, String num) {
		// TODO Auto-generated method stub
		return  scoreDao.getStudentRankCountBeyondParamsByStudentId(studentId, Integer.parseInt(num));
	}

	@Override
	public String formatScoreStr(ScoreEntity scoreEntity) {
		// TODO Auto-generated method stub
		StringBuffer str = new StringBuffer();
		str.append("\n");
		str.append(formatSubjectScoreStr("语文",scoreEntity.getChinese(),scoreEntity.getChineserank()));
		str.append(formatSubjectScoreStr("数学",scoreEntity.getMath(),scoreEntity.getMathrank()));
		str.append(formatSubjectScoreStr("英语",scoreEntity.getEnglish(),scoreEntity.getEnglishrank()));
		str.append(formatSubjectScoreStr("政治",scoreEntity.getPolitical(),scoreEntity.getPoliticalrank()));
		str.append(formatSubjectScoreStr("历史",scoreEntity.getHistory(),scoreEntity.getHistoryrank()));
		str.append(formatSubjectScoreStr("地理",scoreEntity.getGeo(),scoreEntity.getGeorank()));
		str.append(formatSubjectScoreStr("总分",scoreEntity.getTotalscore(),String.valueOf(scoreEntity.getRank())));
		return str.toString();
	}
	
	private String formatSubjectScoreStr(String subjectName,double score,String rank)
	{
		if(score==0)
			return "";
		StringBuffer str = new StringBuffer();
		str.append(subjectName+"：");
		str.append(StringTools.addZeroForNum(String.valueOf(score), 6, " ").replace(".0", ""));
		str.append("		");//18
		str.append("排名：");
		str.append(StringTools.addZeroForNum(rank, 3, " "));
		str.append("\n");
		return str.toString();
	}
}